﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics.Contracts;
using System.ComponentModel.Composition;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(ICustomerRepository))]
    public partial class EntityRepositories:ICustomerRepository
    {

       
        public IQueryable<ICustomer> Customers
        {
            get
            {
                return this.DataReaderContext.Customers;
            }
        }

        public void Post(ICustomer customer)
        {
            Contract.Requires(customer != null);

            var item = new Customer()
            {
                Birthday = customer.Birthday,
                City = customer.City,
                CountryCode = customer.Country==null?null:customer.Country.CountryCode,
                CreateDate = customer.CreateDate,
                FirstName = customer.FirstName,
                Gender = customer.Gender,
                LastName = customer.LastName,
                MailAddress = customer.MailAddress,
                MiddleName = customer.MiddleName,
                Mobile = customer.Mobile,
                SavingPoint = customer.SavingPoint,
                StarLevel = customer.StarLevel,
                Telphone = customer.Telphone,
                UserName = customer.UserName
            };

            if (customer.DefaultAddress == null)
            {
                item.DefaultAddressId = null;
            }
            else
            {
                item.DefaultAddressId = customer.DefaultAddress.Id;
            }

            this.DataWriterContext.Customers.InsertOnSubmit(item);
            this.DataWriterContext.SubmitChanges();

            customer.Id = item.Id;
        }

        public void Put(ICustomer customer)
        {
            Contract.Requires(customer != null);

            var item = this.DataWriterContext.Customers
                .Where(i => i.Id == customer.Id)
                .FirstOrDefault();

            if (item == null)
            {
                throw new KeyNotFoundException("Customer:" + customer.Id);

            }

            item.Birthday = customer.Birthday;
            item.City = customer.City;
            item.CountryCode = customer.Country == null ? null : customer.Country.CountryCode;
            item.CreateDate = customer.CreateDate;
            item.FirstName = customer.FirstName;
            item.Gender = customer.Gender;
            item.LastName = customer.LastName;
            item.MailAddress = customer.MailAddress;
            item.MiddleName = customer.MiddleName;
            item.Mobile = customer.Mobile;
            item.SavingPoint = customer.SavingPoint;
            item.StarLevel = customer.StarLevel;
            item.Telphone = customer.Telphone;
            item.UserName = customer.UserName;


            if (customer.DefaultAddress == null)
            {
                item.DefaultAddressId = null;
            }
            else
            {
                item.DefaultAddressId = customer.DefaultAddress.Id;
            }
            item.CreateDate = customer.CreateDate;

            this.DataWriterContext.SubmitChanges();
        }

        public void Delete(ICustomer customer)
        {
            if (customer != null)
            {
                var item = this.DataWriterContext.Customers
                .Where(i => i.Id == customer.Id)
                .FirstOrDefault();

                if (item != null)
                {
                    this.DataWriterContext.Customers.DeleteOnSubmit(item);
                    this.DataWriterContext.SubmitChanges();
                }
            }
        }

    }
}
